package com.weilele.mvvm.utils.android_r

import android.app.Activity
import android.graphics.Insets
import android.os.Build
import android.os.CancellationSignal
import android.view.*
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import com.weilele.mvvm.R
import com.weilele.mvvm.logE
import com.weilele.mvvm.utils.activity.getScreenInfo
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.utils.safeGet

@RequiresApi(Build.VERSION_CODES.R)
fun Activity.testSize() {
    val view = window.decorView.findViewById<FrameLayout>(android.R.id.content)
    window.setDecorFitsSystemWindows(false)
    view.setOnApplyWindowInsetsListener { v, insets ->
        //状态栏
        val statusBars = insets.getInsets(WindowInsets.Type.statusBars())
        //导航栏
        val navigationBars = insets.getInsets(WindowInsets.Type.navigationBars())
        //等绘制完成再去获取就是 true，这个稍微比较坑一点
        val navigationBarsVisible = insets.isVisible(WindowInsets.Type.navigationBars())
        //键盘
        val ime = insets.getInsets(WindowInsets.Type.ime())
        val info = getScreenInfo()
        insets
    }

    view.setWindowInsetsAnimationCallback(object :
        WindowInsetsAnimation.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
        override fun onProgress(
            insets: WindowInsets,
            runningAnimations: MutableList<WindowInsetsAnimation>
        ): WindowInsets {
            logE(
                "ime:" + insets.getInsets(WindowInsets.Type.ime()).top +
                        " --- " + insets.getInsets(WindowInsets.Type.ime()).bottom
            )
            return insets
        }

        override fun onStart(
            animation: WindowInsetsAnimation,
            bounds: WindowInsetsAnimation.Bounds
        ): WindowInsetsAnimation.Bounds {
            return super.onStart(animation, bounds)
        }

        override fun onEnd(animation: WindowInsetsAnimation) {
            super.onEnd(animation)
        }

        override fun onPrepare(animation: WindowInsetsAnimation) {
            super.onPrepare(animation)
        }
    })
    view.windowInsetsController?.controlWindowInsetsAnimation(
        WindowInsets.Type.ime(),
        -1,
        LinearInterpolator(),
        // A cancellation signal, which allows us to cancel the request to control
        CancellationSignal(),
        // The WindowInsetsAnimationControlListener
        object : WindowInsetsAnimationControlListener {
            override fun onReady(controller: WindowInsetsAnimationController, types: Int) {
                view.onClick {
                    controller.setInsetsAndAlpha(Insets.of(0, 0, 0, 200), 0.5f, 0.5f)
                }
            }

            override fun onFinished(controller: WindowInsetsAnimationController) {

            }

            override fun onCancelled(controller: WindowInsetsAnimationController?) {
            }
        }
    )
}

fun Window.getContentView(): FrameLayout = decorView.findViewById(android.R.id.content)

fun Window?.fullScreen(isFullScreen: Boolean) {
    val window = this ?: return
    val lp = window.attributes
    if (isFullScreen) {
        // 延伸显示区域到耳朵区
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            window.attributes = lp
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            decorView.windowInsetsController?.apply {
                systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                hide(WindowInsets.Type.statusBars())
                hide(WindowInsets.Type.navigationBars())
                setDecorFitsSystemWindows(false)
            }
        } else {
            // 允许内容绘制到耳朵区
            val decorView = window.decorView
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
            if (decorView.getTag(R.id.tag_save_params_mvvm) == null) {
                decorView.setTag(R.id.tag_save_params_mvvm, decorView.systemUiVisibility)
            }
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
        }
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
            window.attributes = lp
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            decorView.windowInsetsController?.apply {
                show(WindowInsets.Type.statusBars())
                show(WindowInsets.Type.navigationBars())
                setDecorFitsSystemWindows(true)
            }
        } else {
            clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
            decorView.getTag(R.id.tag_save_params_mvvm).safeGet<Int>()?.let {
                decorView.systemUiVisibility = it
            }
        }
    }
}

fun View?.fullScreen(isFullScreen: Boolean) {
    val decorView = this ?: return
    if (isFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            decorView.windowInsetsController?.apply {
                systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                hide(WindowInsets.Type.statusBars())
                hide(WindowInsets.Type.navigationBars())
                decorView.fitsSystemWindows = false
            }
        } else {
            // 允许内容绘制到耳朵区
            if (decorView.getTag(R.id.tag_save_params_mvvm) == null) {
                decorView.setTag(R.id.tag_save_params_mvvm, decorView.systemUiVisibility)
            }
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
        }
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            decorView.windowInsetsController?.apply {
                show(WindowInsets.Type.statusBars())
                show(WindowInsets.Type.navigationBars())
                decorView.fitsSystemWindows = true
            }
        } else {
            decorView.getTag(R.id.tag_save_params_mvvm).safeGet<Int>()?.let {
                decorView.systemUiVisibility = it
            }
        }
    }
}

fun Activity?.fullScreen(isFullScreen: Boolean) {
    this?.window?.fullScreen(isFullScreen)
}